﻿
function withjQuery(callback, safe) {
    if (typeof (jQuery) == "undefined") {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
        document.getElementsByTagName("head")[0].appendChild(script);
        //                if (safe) {
        //                    var cb = document.createElement("script");
        //                    cb.type = "text/javascript";
        //                    cb.textContent = "jQuery.noConflict();(" + callback.toString() + ")(jQuery, window);";
        //                    if (typeof window.addEventListener != "undefined") {
        //                        script.addEventListener('load', function () {
        //                            document.getElementsByTagName("head")[0].appendChild(cb);
        //                        });
        //                        
        //                    }
        //                    else {
        //                        script.attachEvent('onreadystatechange', function () {
        //                            if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') {
        //                                document.getElementsByTagName("head")[0].appendChild(cb);
        //                                callback(jQuery, window);
        //                                detachEvent("onreadystatechange");
        //                            }
        //                        });
        //                    }
        //                } else {
        // }
        var dollar = typeof ($) == "undefined" ? undefined : $;
        if (typeof window.addEventListener != "undefined") {
            script.addEventListener('load', function () {
                jQuery.noConflict();
                $ = dollar;
                callback(jQuery, window);
            });
        } else {
            var readyStateChange = function () {
                if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') {
                    jQuery.noConflict();
                    $ = dollar;
                    if (callback && typeof callback === "function") {
                        callback(jQuery, window);
                    }
                    detachEvent("onreadystatechange", readyStateChange);
                }
            };

            script.attachEvent('onreadystatechange', readyStateChange);
        }
    } else {
        setTimeout(function () {
            //Firefox supports
            callback(jQuery, typeof window.unsafeWindow === "undefined" ? window : window.unsafeWindow);
        }, 30);
    }
}